package com.yanqu.road.server.logic.thread.translate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ChannelInfo;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.sdk.TranslateLanguageMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @Author wpj
 * @Date 2021/4/15
 */
public class TranslateLanguageTask extends GeneralTask {

    GamePlayer player;
    String content;
    String language;
    long msgIndex;

    private static final Logger log = LogManager.getLogger(TranslateLanguageTask.class);

    public TranslateLanguageTask(GamePlayer player, String content, String language, long msgIndex) {
        this.player = player;
        this.content = content;
        this.language = TranslateLanguageMgr.languageCodeTransfer(language);
        this.msgIndex = msgIndex;
    }

    @Override
    public void doRun() {
            try {
                if (ServerListMgr.isOpenTranslateChat()) {
                    ChannelInfo channelConfig = ServerListMgr.getChannelInfoMap().get(ServerListMgr.getServerInfo(ConfigHelper.getLong("serverId")).getChannelId());
                    String afterContent = TranslateLanguageMgr.getContent(this.content, language);
                    if (afterContent == null) {
                        SortedMap<String, String> paramMap = new TreeMap<>();
                        paramMap.put("q", content);
                        paramMap.put("target", language);
                        String result = HttpHelper.doPost(channelConfig.getTranslateUrl(), paramMap, "UTF-8");
                        JSONObject jsonObject = JSONObject.parseObject(result);
                        JSONObject data = jsonObject.getJSONObject("data");
                        JSONArray translations = data.getJSONArray("translations");
                        JSONObject text = (JSONObject) translations.get(0);
                        afterContent = text.getString("translatedText");
                        TranslateLanguageMgr.addTranslateContent(content, language, afterContent);
                    }
                    WorldMessageProto.TranslationRespMsg.Builder msg = WorldMessageProto.TranslationRespMsg.newBuilder();
                    msg.setRet(0);
                    msg.setAfterTranslateContent(afterContent);
                    msg.setMsgIndex(msgIndex);
                    player.sendPacket(ClientProtocol.U_TRANSLATE_LANGUAGE, msg);
                }
            } catch (Exception ex) {
                log.error(ex);
            }
        }
    }
